<template>
  <div class="baseinfo" v-title data-title="ED仪器生产制造立项界面">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="项目编号" prop="projectNo" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
            {{form.projectNo}}
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            ref="projectName"
            label="项目名称"
            prop="projectName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.projectName"
              @blur="
                () => {
                  $refs.projectName.onFieldBlur()
                }
              "
              placeholder="请输入有效的项目名称" 
              :disabled="form.status>1" 
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="项目分类"
            prop="projectTypeSubCategory"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <span style="color: #f7a961; margin: 0 6px; font-size: 16px">{{ subName }}</span>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item
            ref="mainDeptName"
            label="主导经营体"
            prop="mainDeptName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input v-model="form.mainDeptName" placeholder="请填写正确的组织架构名称，系统自动校验" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            ref="instrumentName"
            label="仪器设备名称"
            prop="instrumentName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.instrumentName"
              @blur="
                () => {
                  $refs.instrumentName.onFieldBlur()
                }
              "
              placeholder="请填写仪器设备名称" 
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            ref="instrumentType"
            label="仪器设备型号"
            prop="instrumentType"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.instrumentType"
              @blur="
                () => {
                  $refs.instrumentType.onFieldBlur()
                }
              "
              placeholder="请填写仪器设备型号" 
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item
            ref="instrumentNum"
            label="仪器设备数量"
            prop="instrumentNum"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.instrumentNum"
              @blur="
                () => {
                  $refs.instrumentNum.onFieldBlur()
                }
              "
              placeholder="请输入仪器设备数量"
              type="number"
            >
            </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            ref="relationNo"
            label="关联的研发阶段的项目号"
            prop="relationNo"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.relationNo"
              @blur="
                () => {
                  $refs.relationNo.onFieldBlur()
                }
              "
              placeholder="请填写关联的研发阶段的项目号"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="是否签订合同或协议"
            ref="hasSign"
            prop="hasSign"
            :label-col="{ span: 12 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-radio-group v-model="form.hasSign">
              <a-radio :value="true"> 是 </a-radio>
              <a-radio :value="false"> 否 </a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="projectOverview"
            label="项目概况"
            prop="projectOverview"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 21 }"
          >
            <a-textarea
              v-model="form.projectOverview"
              @blur="
                () => {
                  $refs.projectOverview.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
              placeholder="请填写项目概况,500字内。"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <five-to-five ref="fiveForm" :projectBuildId="projectBuildId" :change-id="changeId" @addField="addFiveDataToForm"/>
    </a-form-model>
  </div>
</template>
<script>
import $ from 'jquery'
import { getAction, getActionAsync, postAction, postActionAsync } from '@/api/manage'
import { getParamValFromUrl, strNotNull } from '@/utils/common.js'
import Vue from 'vue'
export default {
  components: {
    FiveToFive: () => import('@/components/tools/projectBuildStup/baseInfo/FiveToFive'),
  },
  props: {
    projectBuildId: {
      Type: String,
      default: '',
    },
    changeId: {
      Type: String,
      default: '',
    },
    selProjectType: {
      Type: String,
      default: '',
    }
  },
  data() {
    return {
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      form: {
        id:'',
        projectClass: '20',
        projectNo: '',
        projectName: '',
        mainDeptName: '',
        mainDeptId: '',
        projectTypeSubCategory: '',
        instrumentName: '',
        instrumentType: '',
        instrumentNum: '',
        relationNo: '',
        hasSign: '', 
        projectOverview: '', 
        status: 0 
      },
      rules: {
        projectName: [
          { required: true, message: '请输入有效的项目名', trigger: 'blur' },
          { min: 1, max: 50, message: '项目名不超过50个字符', trigger: 'blur' },
        ],
        tcProjectRealName: [
          { min: 1, max: 50, message: 'TC项目名称不超过50个字符', trigger: 'blur' }
        ],
        mainDeptName: [
          { required: true, message: '请填写正确的组织架构名称，系统自动校验', trigger: 'blur' }
        ],
        projectTypeSubCategory: [{ required: true, message: '请选择项目分类', trigger: 'change' }],
        instrumentName: [
          { required: true, message: '请输入仪器设备名称', trigger: 'blur' },
          { min: 1, max: 500, message: '仪器设备名称不超过500个字符', trigger: 'blur' },
        ],
        instrumentType: [
          { required: true, message: '请输入仪器设备型号', trigger: 'blur' },
          { min: 1, max: 500, message: '仪器设备型号不超过500个字符', trigger: 'blur' },
        ],
        instrumentNum: [
          { required: true, message: '请填写仪器设备数量', trigger: 'blur' }
        ],
        relationNo: [
          { required: true, message: '请填写关联的研发阶段的项目号', trigger: 'blur' },
          { min: 1, max: 25, message: '项目号不超过25个字符', trigger: 'blur' },
        ],
        hasSign: [{ required: true, message: '请选择是否签订合同或协议', trigger: 'blur' }],
        projectOverview: [
          { required: true, message: '请填写项目概况', trigger: 'blur' },
          { min: 1, max: 500, message: '项目概况不超过500个字符', trigger: 'blur' },
        ],
      },
      projectCategoryOptions: [],
      url: {
        codeInfoList: '/manage/codeInfo/list',
        getProjectByNo: '/projectBuild/getProjectByNo',
        save: '/projectBuild/add',
        queryVOById: '/projectBuild/queryVOById',
      },
      loadFinish: false,
      subName: ''
    }
  },
  computed: {},
  methods: {
    addFiveDataToForm(val,valRef) {
      this.$set(this.form, valRef, val)
    },
    async doSubmit() {
      if(!this.loadFinish){
        layer.message.warning('请等待页面加载完毕')
        return {}  
      }
      var validR=false;
      try{
        validR = await this.$refs.ruleForm.validate();
        validR = await this.$refs.fiveForm.checkFiveWriteStatus()
      }catch(e){
        validR = false;
      }
      console.log('validR',validR);
      this.form.validResult = validR;
      let res=await this.doSave();
        this.projectBuildId=res;
      return {valid:validR,id:res};
    },
    async doSave() {
      let flag=null;
      var keywords = this.form.newProductKeyword
      if (strNotNull(keywords)) {
        let keywordLen = keywords.split('；').filter((x) => !!x).length
        if (keywordLen < 3) {
          this.$message.warning('请填写至少3组关键词，关键词之间用中文【；】隔开');
          return flag;
        }
      }

      this.form.changeId=this.changeId;
      var formData = JSON.parse(JSON.stringify(this.form));

      var vm = this
      await postActionAsync(this.url.save, formData)
        .then((res) => {
          if (res.code == 200) {
            vm.form.id = res.result.id;
            vm.form.projectNo = res.result.projectNo;
            this.$message.success('保存成功')
            return flag=res.result.id;
          } else {
            this.$message.warning('保存数据出错:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('保存数据出错:' + res.message)
        })
        return flag;
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    //初始化项目子类
    initProjectCategoryOptions() {
      var vm = this
      getAction(this.url.codeInfoList, { type: '项目子类', isdel: 0, parentid: vm.form.projectClass,pageSize:-1 })
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              list.records.map((item) => {
                vm.projectCategoryOptions.push({ value: item.code, label: item.name })
              })
              if (vm.projectCategoryOptions.length === 1) {
                vm.form.projectTypeSubCategory = vm.projectCategoryOptions[0].value
              }
            }
          } else {
            this.$message.warning('查询项目子类失败:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('查询项目子类失败:' + res.message)
        })
    },
    //如果有id，则加载已填写信息
    initData() {
      if (this.projectBuildId != null && this.projectBuildId != '') {
        var vm = this
        getAction(this.url.queryVOById, { id: this.projectBuildId,changeId: this.changeId })
          .then((res) => {
            if (res.code == 200) {
              var result = res.result
              if (result != null) {
                this.form = result
                this.initSubName()
                this.loadFinish = true
              } else {
                vm.$message.warning('初始化项目信息为空')
              }
            } else {
              this.$message.warning('初始化项目信息失败:' + res.message)
            }
          })
          .catch((res) => {
            this.$message.warning('初始化项目信息失败:' + res.message)
          })
      }else{
        this.loadFinish = true
      }
    },
    initSubName () {
      var vm = this
      getAction(this.url.codeInfoList, { type: '项目子类', isdel: 0,parentid:vm.form.projectClass,code:vm.form.projectType,pageSize:-1})
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              this.subName = list.records[0].name
            }
          } else {
            this.$message.warning('项目子类:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('项目子类:' + res.message)
        })
    },
  },
  mounted() {
    if (this.projectBuildId == null || this.projectBuildId == '') {
        this.form.projectType = this.selProjectType
        this.form.projectTypeSubCategory = this.selProjectType
        this.form.tcProjectRealName = this.$route.query.tcname
        this.initSubName()
     }

    this.initProjectCategoryOptions()
    this.initData()
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-btn-primary {
  width: 85px;
  height: 30px;
  border-radius: 2px;
  background: #4095e4;
  margin-right: 10px;
}
/deep/ .default {
  width: 85px;
  height: 30px;
  border-radius: 2px;
}
.baseinfo {
  background-color: #fff;
  border: 1px solid #eaeaea;
  padding: 30px;
  .btn {
    display: flex;
    justify-content: center;
  }
}
</style>